package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioProperties minioProperties;
    @Override
    public String upload(MultipartFile multipartFile) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        //获取原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        originalFilename = sdf.format(new Date()) + "/" + (UUID.randomUUID().toString().replaceAll("-", "")) + "_" + originalFilename;
        minioClient.putObject(PutObjectArgs.builder().bucket(minioProperties.getBucketName())
                .stream(multipartFile.getInputStream(),multipartFile.getSize(),-1)
                .object(originalFilename)
                .contentType(multipartFile.getContentType())
                .build());
        return String.format("%s/%s",minioProperties.getEndpoint(),minioProperties.getBucketName()+"/"+originalFilename);

    }
}
